package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class XMLParsingStilling {
	ArrayList<Item> items = new ArrayList<Item>();
	
	public ArrayList<Item> StillingParser(File file) {
	    try    	{
	    	
	    	items = new ArrayList<util.Item>();
	    	
	    	FileInputStream is = new FileInputStream(file);
			//Standard Java bruger xpp3-1.1.4.jar fra http://www.extreme.indiana.edu/xgws/xsoap/xpp/
			XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
			XmlPullParser xpp=factory.newPullParser();
			 
		//	XmlPullParser xpp = android.util.Xml.newPullParser();  // Android-kald til at oprette parser
			//xpp.setInput(is, "UTF-8"); // evt "ISO-8859-1"
			xpp.setInput(new InputStreamReader(is));

			//Bank bank = null; // Husker den aktuelle bank
			Item item = null; // Husker den aktuelle kunde

			int eventType = xpp.getEventType();
					
			while (eventType != XmlPullParser.END_DOCUMENT)
			{
				if (eventType == XmlPullParser.START_TAG)
				{
					item = new Item();
					String s;
					String tag = xpp.getName();
					
					try {
						if ("item".equals(tag))
						//if ("title".equals(tag))
						{
							//skip item
							xpp.nextTag();
							
							//PLACERING
							s = xpp.nextText();
							item.placering = s;
							
							//HOLD
							xpp.nextTag();
							s = xpp.nextText();
							item.hold = s;

							//ANTAL_KAMPE
							xpp.nextTag();
							s = xpp.nextText();
							item.antal_kampe = s;

							//VUNDNE
							xpp.nextTag();
							s = xpp.nextText();
							item.vundne = s;
							
							//UAFGJORTE
							xpp.nextTag();
							s = xpp.nextText();
							item.uafgjorte = s;
							
							//TABTE
							xpp.nextTag();
							s = xpp.nextText();
							item.tabte = s;
							
							//MAAL
							xpp.nextTag();
							s = xpp.nextText();
							item.maal = s;
							
							//M�L_IMOD
							xpp.nextTag();
							s = xpp.nextText();
							item.maal_imod = s;
							
							//POINT			
							xpp.nextTag();
							s = xpp.nextText();
							item.point = s;
							
							//add to arraylist
							items.add(item);
						}
					} catch (Exception e) {
						e.printStackTrace();
						System.out.println("An Error occured. The program crashed: " + e.toString());
						//contact app admin
						//show error messages.
					}
				}
				eventType = xpp.next();
			}
			
			is.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
			//tv.append("FEJL:" + ex.toString());
			System.out.println("FEJL:" + ex.toString());
		}
	    return items;
	}

    	public static boolean doesFileExistOnDevice( File directory ) 
        {
 
    		File file = new File(directory + "/StadionApp/" + "stilling.xml");
                if (file.exists()) 
                        return true;
                else
                        return false;
        }

}


	
